using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Tool
{
	public class LoginWebsite : Form
	{
		private System.ComponentModel.IContainer components = null;

		private Label label1;

		private TextBox tb_url;

		private Button bt_open;

		private Label lb_msg;

		private WebBrowser myWebBrowser;

		public LoginWebsite()
		{
			this.InitializeComponent();
			this.MaximumSize = new System.Drawing.Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
			this.MinimumSize = new System.Drawing.Size(800, 600);
		}

		private void LoginWebsite_SizeChanged(object sender, EventArgs e)
		{
			if (base.WindowState == FormWindowState.Maximized && (base.Size.Width > Screen.PrimaryScreen.WorkingArea.Width || base.Size.Height > Screen.PrimaryScreen.WorkingArea.Height))
			{
				base.Location = new System.Drawing.Point(0, 0);
				base.WindowState = FormWindowState.Normal;
				base.Size = new System.Drawing.Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
			}
		}

		private void bt_open_Click(object sender, EventArgs e)
		{
			if (!(this.tb_url.Text.Trim() == ""))
			{
				this.method_0();
			}
			else
			{
				this.lb_msg.Text = "请输入网址";
				this.lb_msg.Visible = true;
				this.tb_url.Focus();
			}
		}

		private void method_0()
		{
			string text = this.tb_url.Text.Trim();
			if (!text.StartsWith("http://") || text.StartsWith("https://"))
			{
				text = "http://" + text;
			}
			try
			{
				this.myWebBrowser.Url = new System.Uri(text);
				this.myWebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.myWebBrowser_DocumentCompleted);
			}
			catch (Exception ex)
			{
				this.myWebBrowser.Document.Body.InnerHtml = ex.ToString();
			}
		}

		private void myWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
		{
			WebBrowser webBrowser = (WebBrowser)sender;
			int count = webBrowser.Document.Links.Count;
			for (int i = 0; i < count; i++)
			{
				if (webBrowser.Document.Links[i].GetAttribute("target") == "_blank")
				{
					webBrowser.Document.Links[i].SetAttribute("target", "_self");
				}
			}
			this.tb_url.Text = webBrowser.Url.ToString();
			this.Text = webBrowser.Document.Title;
		}

		private void tb_url_TextChanged(object sender, EventArgs e)
		{
			this.lb_msg.Text = "";
		}

		private void tb_url_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Return)
			{
				this.bt_open_Click(sender, e);
			}
		}

		private void LoginWebsite_Load(object sender, EventArgs e)
		{
			string text = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
			text += "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
			text += "<head>";
			text += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />";
			text += "<meta HTTP-EQUIV=\"pragma\" CONTENT=\"no-cache\">";
			text += "<meta HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-cache, must-revalidate\">";
			text += "<meta HTTP-EQUIV=\"expires\" CONTENT=\"0\">";
			text += "<title></title>";
			text += "</head>";
			text += "<body style=\"font-family:'微软雅黑', '宋体', Verdana, Tahoma; font-size:15px; color:#555; margin:0px; padding:1px;\">";
			text += "<div style=\"padding:12px;\">";
			text += "<div style=\"font-weight:bold; font-size:14px; padding:8px 8px; border-bottom:1px solid #ccc;\">对于需要登陆后才能抓取的页面，请按以下步骤操作：</div>";
			text += "<div style=\"margin:0px; padding:8px; line-height:20px;\">";
			text += "<div style=\" margin-top:0px;\">1、输入你要抓取页面的网址、 试着打开网址。</div>";
			text += "<div style=\" margin-top:8px;\">2、查看能否打开输入网址的页面。如果打开正常，说明软件已获取到登陆信息，是可以正常抓取到该页面的，请跳过第3步；否则继续第3步。</div>";
			text += "<div style=\" margin-top:8px;\">3、对于打不开输入网址的页面，网站一般会引导你到登陆页面，这时你需要输入帐号和密码登陆即可，登陆成功后系统会自动获取登陆信息。</div>";
			text += "<div style=\" margin-top:8px;\">4、此时系统已能够获取到登陆信息，关闭窗口返回抓取主界面。</div>";
			text += "</div>";
			text += "</div>";
			text += "</body>";
			text += "</html>";
			this.myWebBrowser.DocumentText = text;
		}

		//protected override void vmethod_0(bool bool_0)
		//{
		//	if (bool_0 && this.components != null)
		//	{
		//		this.components.Dispose();
		//	}
		//	base.Dispose(bool_0);
		//}

		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager componentResourceManager = new System.ComponentModel.ComponentResourceManager(typeof(LoginWebsite));
			this.label1 = new Label();
			this.tb_url = new TextBox();
			this.bt_open = new Button();
			this.lb_msg = new Label();
			this.myWebBrowser = new WebBrowser();
			base.SuspendLayout();
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(13, 13);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(65, 12);
			this.label1.TabIndex = 0;
			this.label1.Text = "输入网址：";
			this.tb_url.Location = new System.Drawing.Point(84, 10);
			this.tb_url.Name = "tb_url";
			this.tb_url.Size = new System.Drawing.Size(500, 21);
			this.tb_url.TabIndex = 1;
			this.tb_url.TextChanged += new EventHandler(this.tb_url_TextChanged);
			this.tb_url.KeyDown += new KeyEventHandler(this.tb_url_KeyDown);
			this.bt_open.Location = new System.Drawing.Point(590, 8);
			this.bt_open.Name = "bt_open";
			this.bt_open.Size = new System.Drawing.Size(75, 23);
			this.bt_open.TabIndex = 2;
			this.bt_open.Text = "打开网址";
			this.bt_open.UseVisualStyleBackColor = true;
			this.bt_open.Click += new EventHandler(this.bt_open_Click);
			this.lb_msg.AutoSize = true;
			this.lb_msg.ForeColor = System.Drawing.Color.Red;
			this.lb_msg.Location = new System.Drawing.Point(671, 13);
			this.lb_msg.Name = "lb_msg";
			this.lb_msg.Size = new System.Drawing.Size(0, 12);
			this.lb_msg.TabIndex = 3;
			this.myWebBrowser.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.myWebBrowser.Location = new System.Drawing.Point(0, 37);
			this.myWebBrowser.MinimumSize = new System.Drawing.Size(20, 20);
			this.myWebBrowser.Name = "myWebBrowser";
			this.myWebBrowser.Size = new System.Drawing.Size(1008, 545);
			this.myWebBrowser.TabIndex = 4;
			base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
			base.AutoScaleMode = AutoScaleMode.Font;
			this.AutoScroll = true;
			base.ClientSize = new System.Drawing.Size(1008, 582);
			base.Controls.Add(this.myWebBrowser);
			base.Controls.Add(this.lb_msg);
			base.Controls.Add(this.bt_open);
			base.Controls.Add(this.tb_url);
			base.Controls.Add(this.label1);
			//base.Icon = (System.Drawing.Icon)componentResourceManager.GetObject("$this.Icon");
			base.Name = "LoginWebsite";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterParent;
			this.Text = "浏览器";
			base.Load += new EventHandler(this.LoginWebsite_Load);
			base.SizeChanged += new EventHandler(this.LoginWebsite_SizeChanged);
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
}
